package cn.tedu._05mvcboot02.controller;

import cn.tedu._05mvcboot02.mapper.UserMapper;
import cn.tedu._05mvcboot02.pojo.dto.InsertDTO;
import cn.tedu._05mvcboot02.pojo.entity.User;
import cn.tedu._05mvcboot02.pojo.vo.UserListVO;
import cn.tedu._05mvcboot02.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/*1确定是否需要vo或者dto
* 2梳理业务逻辑,确定接口
* 3完成接口方法和xml配置sql语句
* 4完成controller业务逻辑
* 5重启工程,测试*/
//@Controller
/*@RestController
 组合注解,相当于@Controller+@ResponseBody
 添加注解后,类中方法上无需再添加@ResponseBody
 */
@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("/insert")
    //@ResponseBody
    public String insert(InsertDTO insertDTO){
        User user =new User();
        BeanUtils.copyProperties(insertDTO , user);
        user.setCreated(new Date());
        userMapper.insert(user);
        return "添加成功";
    }
    @GetMapping("/select")
    //@ResponseBody
    public List<UserListVO> userListVO(){
        return userMapper.userList();
    }
    @GetMapping("/delete")
    //@ResponseBody
    public String delete(Integer id){
        userMapper.delete(id);
        return "删除成功";
    }
    @PostMapping("/update")
    //@ResponseBody
    public String update(User user){
        userMapper.update(user);
        return "修改成功";
    }
    /*@PathVariable声明路径参数,在路径path中获取参数*/
    @GetMapping("selectById/{uid}")
    public UserVO selectById(@PathVariable Integer uid){
        return userMapper.selectById(uid);
    }
}
